<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <table border="1">
        <caption>员工列表</caption>
        <tr>
            <th>id</th>
            <th>姓名</th>
            <th>工资</th>
            <th>工作</th>
            <th>操作</th>
        </tr>
        <tr v-for="emp in arr">
            <td>{{emp.id}}</td>
            <td>{{emp.name}}</td>
            <td>{{emp.sal}}</td>
            <td>{{emp.job}}</td>
            <!--javascript:void(0) 废掉自带的跳转功能-->
            <td>
                <!--如果元素属性里面出现变量，必修使用属性绑定-->
                <a :href= "'/update.html?id='+emp.id">修改</a>
                <a href="javascript:void(0)" @click="del(emp.id)">删除</a>
            </td>
        </tr>
    </table>
</div>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

<script>
    let v = new Vue({
        el: "body>div",
        data: {
            arr: [
                // 测试假数据
                // {id: 1, name: "刘备", sal: 3000, job: "销售"},
                // {id: 2, name: "刘备", sal: 3000, job: "销售"},
                // {id: 3, name: "刘备", sal: 3000, job: "销售"}
            ]
        },
        methods: {
            del(id) {
                if (confirm("您确认删除此员工吗？")) {
                    // 发出异步的删除请求
                    axios.get("delete?id=" + id).then(function (response) {
                        //删除完成后  页面重刷新
                        location.reload();
                    })
                }

            }
        },
        // created 方法是vue对象中提供的方法，会在创建Vue对象时自动调用，通常进入页面发请求的代码写到这里
        created: function () {
            // 发出异步get请求获取数据
            axios.get("/select").then(function (response) {

                v.arr = response.data;
            })
        }
    })
</script>
</body>
</html>